package cn.stylefeng.guns.modular.business.controller;

import cn.stylefeng.guns.modular.business.pojo.UseCollectRequest;
import cn.stylefeng.guns.modular.business.service.UseCollectService;
import cn.stylefeng.roses.kernel.rule.pojo.response.ErrorResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.ResponseData;
import cn.stylefeng.roses.kernel.rule.pojo.response.SuccessResponseData;
import cn.stylefeng.roses.kernel.scanner.api.annotation.ApiResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.GetResource;
import cn.stylefeng.roses.kernel.scanner.api.annotation.PostResource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 用户收藏控制器
 *
 * @author zhangyaowen
 * @date 2021/11/17 15:51
 */
@RestController
@ApiResource(name = "用户收藏")
public class UseCollectController {

    @Resource
    private UseCollectService useCollectService;

    /**
     * 添加
     *
     * @author zhangyaowen
     * @date 2021/11/17 15:51
     */
    @GetResource(name = "添加收藏", path = "/useCollect/add")
    public ResponseData add(String collectTitle,String collectType) {

        try {
            return new SuccessResponseData(useCollectService.add(collectTitle,collectType));

        }catch (Exception exception){
            return new ErrorResponseData("500","已收藏");
        }

    }

//    public ResponseData add(@RequestBody @Validated(UseCollectRequest.add.class) UseCollectRequest useCollectRequest) {
//
//        useCollectService.add(useCollectRequest);
//        return new SuccessResponseData();
//    }

    /**
     * 删除
     *
     * @author zhangyaowen
     * @date 2021/11/17 15:51
     */
    @PostResource(name = "删除", path = "/useCollect/delete")
    public ResponseData delete(@RequestBody @Validated(UseCollectRequest.delete.class) UseCollectRequest useCollectRequest) {
        useCollectService.del(useCollectRequest);
        return new SuccessResponseData();
    }

    /**
     * 编辑
     *
     * @author zhangyaowen
     * @date 2021/11/17 15:51
     */
    @PostResource(name = "编辑", path = "/useCollect/edit")
    public ResponseData edit(@RequestBody @Validated(UseCollectRequest.edit.class) UseCollectRequest useCollectRequest) {
        useCollectService.edit(useCollectRequest);
        return new SuccessResponseData();
    }

    /**
     * 查看详情
     *
     * @author zhangyaowen
     * @date 2021/11/17 15:51
     */
    @GetResource(name = "查看详情", path = "/useCollect/detail")
    public ResponseData detail(@Validated(UseCollectRequest.detail.class) UseCollectRequest useCollectRequest) {
        return new SuccessResponseData(useCollectService.detail(useCollectRequest));
    }

    /**
     * 获取列表
     *
     * @author zhangyaowen
     * @date 2021/11/17 15:51
     */
    @GetResource(name = "获取列表", path = "/useCollect/list")
    public ResponseData list(UseCollectRequest useCollectRequest) {
        return new SuccessResponseData(useCollectService.findList(useCollectRequest));
    }

    /**
     * 获取列表（带分页）
     *
     * @author zhangyaowen
     * @date 2021/11/17 15:51
     */
    @GetResource(name = "分页查询", path = "/useCollect/page")
    public ResponseData page(String title) {
        UseCollectRequest useCollectRequest = new UseCollectRequest();
        useCollectRequest.setCollectTitle(title);
        return new SuccessResponseData(useCollectService.findPage(useCollectRequest));
    }

    /**
     * 获取收藏状态
     *
     * @author zhangyaowen
     * @date 2021/11/17 15:51
     */
    @GetResource(name = "获取收藏状态", path = "/useCollect/findCollectType")
    public ResponseData findCollectType() {
        if( useCollectService.findCollectType() != null){
            return new SuccessResponseData(useCollectService.findCollectType());
        }else{
            return new SuccessResponseData("500","未收藏",null);

        }
    }

    /**
     * 取消收藏
     *
     * @author zhangyaowen
     * @date 2021/11/17 15:51
     */
    @GetResource(name = "取消收藏", path = "/useCollect/cancleCollect")
    public ResponseData cancleCollect(String id) {
        UseCollectRequest useCollectRequest = new UseCollectRequest();
        Long idc = Long.parseLong(id);
        useCollectRequest.setId(idc);
        useCollectService.del(useCollectRequest);
        return new SuccessResponseData();
    }
}